﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace WPFStudy.Extensions
{
    /// <summary>
    /// Binding值获取扩展
    /// </summary>
    public class BindingEvaluatorExtensions : FrameworkElement
    {
        /// <summary>
        /// <code>
        /// Binding b1 = new Binding
        /// {
        ///     Source = new Entity { value = "e" },
        ///     Path = new PropertyPath("value")
        ///  };
        /// //get value
        /// BindingEvaluatorExtensions be = new BindingEvaluatorExtensions(b1);
        /// var bindingvalue = be.Value;
        /// </code>
        /// </summary>
        public object Value
        {
            get
            {
                Value = DependencyProperty.UnsetValue;
                SetBinding(ValueProperty, _binding);
                return (object)GetValue(ValueProperty);
            }
            private set { SetValue(ValueProperty, value); }
        }
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(object), typeof(BindingEvaluatorExtensions), null);

        BindingBase _binding;

        /// <summary>
        /// <code>
        /// Binding b1 = new Binding
        /// {
        ///     Source = new Entity { value = "e" },
        ///     Path = new PropertyPath("value")
        ///  };
        /// //get value
        /// BindingEvaluatorExtensions be = new BindingEvaluatorExtensions(b1);
        /// be.DataContext = obj;
        /// var bindingvalue = be.Value;
        /// </code>
        /// </summary>
        /// <param name="binding"></param>
        public BindingEvaluatorExtensions(BindingBase binding)
        {
            _binding = binding;
        }

        /// <summary>
        /// <code>
        /// Binding b1 = new Binding
        /// {
        ///     Source = new Entity { value = "e" },
        ///     Path = new PropertyPath("value")
        ///  };
        /// //get value
        /// BindingEvaluatorExtensions be = new BindingEvaluatorExtensions(b1);
        /// be.DataContext = obj;
        /// var bindingvalue = be.Value;
        /// </code>
        /// </summary>
        /// <param name="binding"></param>
        public BindingEvaluatorExtensions(BindingBase binding, object dataContext = null):this(binding)
        {
            DataContext = dataContext;
        }
    }
}
